﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TSCC.WinApp
{
    public partial class frmMain : Form
    {
        bool isShootDown = false;
        TSCC.Interface.iUser usr = null;
        frmLogin frm = null;
        public frmMain()
        {
            InitializeComponent();
        }

        protected void Login()
        {
            bool OK = true;
            frm = new frmLogin();
            for (int i = 1; i <= 3; i++)
            {
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    usr = TSCC.Service.Utility.Login(frm.GetCSRNO(), frm.GetPassword());
                    if (usr != null)
                    {
                        OK = true;
                        break; 
                    }
                    else
                    {
                        MessageBox.Show(string.Format("登入錯誤 第{0}次",i.ToString()));
                        OK = false;
                    }
                }
                else
                {                    
                    shootDown();
                    break;
                }               
            }
            frm.Dispose();
            if (!OK)
            {
                isShootDown = true;
                shootDown();
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            Login();
            if (!isShootDown)
            {
                showMenu();
            }            
        }


        /// <summary>
        /// 依權限長出menu 出來
        /// </summary>
        protected void showMenu()
        {
            ToolStripMenuItem itm = null;
            itm = getCSRMenuItem();
            menuMain.Items.Add(itm);
            if (!usr.IsCSR())
            {
                itm = getMMGMenuItem();
                menuMain.Items.Add(itm);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shootDown();
        }

        protected void shootDown()
        {
            isShootDown = true;
            Application.Exit();
        }

        protected ToolStripMenuItem getMMGMenuItem()
        {
            ToolStripMenuItem itm = new ToolStripMenuItem("報表");
            itm.Click += new EventHandler(itm_Click); 
            return itm;
        }

        protected ToolStripMenuItem getCSRMenuItem()
        {
            ToolStripMenuItem itm = new ToolStripMenuItem("例外資訊登錄");
            itm.Click += new EventHandler(itm_Click);
            return itm;
        }

        void itm_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem itm = sender as ToolStripMenuItem;
            if (itm.Text.Equals("例外資訊登錄"))
            {
                frmExceptionLog frm = new frmExceptionLog(usr);
                frm.Show();
            }
            else
            {
                frmReport frm = new frmReport();
                frm.Show();
            }
           
        }

        private void tESTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmTest frm = new frmTest();
            frm.Show();
        }

    }
}
